//
//  MainViewController.m
//  JinYong_iOS_Store
//
//  Created by 王厚一 on 2017/10/17.
//  Copyright © 2017年 why. All rights reserved.
//

#import "MainViewController.h"
#import "OrderViewController.h"
#import "MineInfoViewController.h"

@interface MainViewController ()

@property (nonatomic,strong) UIImageView *logoImageView;//头像
@property (nonatomic,strong) UILabel *nameLabel;//店名
@property (nonatomic,strong) UILabel *moneyLabel;//余额
@property (nonatomic,strong) UILabel *orderLabel;//订单
@property (nonatomic,strong) UILabel *turnoverLabel;//营业额
@property (nonatomic,strong) UILabel *subMoneyLabel;//可提现
@property (nonatomic,strong) UILabel *subOrderLabel;//昨日订单
@property (nonatomic,strong) UILabel *subTurnoverLabel;//昨日营业额
@property (nonatomic,strong) dispatch_source_t timer;//定时器

@end

@implementation MainViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if (kUserLoginResult) {
        [self loadData];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createTopView];//创建视图
}

#pragma mark - -------------------------数据请求----------------------------
- (void)loadData {
    [[URLFather sharedInstance] loadMainDataWithSuccess:^(NSDictionary *result) {
        if ([Pub resultSuccess:result]) {
            NSDictionary * dict = result[@"data"];
            [NSUDUD setValue:[NSString stringWithFormat:@"%@",[dict objectForKey:@"shop_logo"]] forKey:kLogo];
            [NSUDUD setValue:[NSString stringWithFormat:@"%@",[dict objectForKey:@"shop_name"]] forKey:kName];
            
            [self.logoImageView sd_setImageWithURL:kRequestLogo];
            self.nameLabel.text = kRequestName;
            self.moneyLabel.text = [NSString stringWithFormat:@"￥%@", dict[@"user_money"]];
            self.orderLabel.text = [NSString stringWithFormat:@"%@", dict[@"sales_num_today"]];
            self.turnoverLabel.text = [NSString stringWithFormat:@"%@", dict[@"order_sales_today"]];
            self.subMoneyLabel.text = [NSString stringWithFormat:@"可提现%@", dict[@"user_money_cash"]];
            self.subOrderLabel.text = [NSString stringWithFormat:@"昨日%@单", dict[@"sales_num_yes"]];
            self.subTurnoverLabel.text = [NSString stringWithFormat:@"昨日%@", dict[@"order_sales_yes"]];
        }
        else
        {
            kShowText(result[@"msg"]);
        }
    } failure:^(NSError *error) {
        kShowError(@"请求失败");
    }];
}

#pragma mark - -------------------------视图部分----------------------------
- (void)createTopView {
    //背景图13916347323 123456
    UIImageView * backImageView = ({
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(0), ActualW(375), ActualH(245))];
        imageView.image = [UIImage imageNamed:@"background"];
        imageView;
    });
    [self.view addSubview:backImageView];
    
    //设置按钮
    UIButton * settingBtn = ({
        UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(ActualW(16), ActualH(29.5), ActualW(25), ActualH(25))];
        [button addTarget:self action:@selector(settingAction) forControlEvents:UIControlEventTouchUpInside];
        [button setBackgroundImage:[UIImage imageNamed:@"setting_white"] forState:UIControlStateNormal];
        button;
    });
    [self.view addSubview:settingBtn];
    
    //标题
    UILabel * titleLabel = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(150), ActualH(34), ActualW(75), ActualH(17))];
        label.textColor = RGB(255, 255, 255);
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"个人中心";
        label.font = FontX(FONTSIZE(16));
        label;
    });
    [self.view addSubview:titleLabel];
    
    //头像
    self.logoImageView = ({
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(ActualW(17), ActualH(79.5), ActualW(63), ActualH(63))];
        imageView.image = [UIImage imageNamed:@"picture"];
        imageView.layer.cornerRadius = ActualW(31.5);
        imageView.layer.masksToBounds = YES;
        imageView;
    });
    [self.view addSubview:self.logoImageView];
    
    //店名
    self.nameLabel = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(98.5), ActualH(91.5), ActualW(270), ActualH(18))];
        label.textColor = RGB(255, 255, 255);
        label.textAlignment = NSTextAlignmentLeft;
        label.text = @"";
        label.font = FontX(FONTSIZE(17));
        label;
    });
    [self.view addSubview:self.nameLabel];
    
    //中间部分信息栏
    NSArray * topArray = @[@"可用余额",@"今日订单",@"今日营业额"];
    for (int i = 0; i < 3; i++) {
        UIView * subView = ({
            UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW((375/3.0)) * i, ActualH(166), ActualW((375/3.0)), ActualH(80))];
            view.backgroundColor = [UIColor clearColor];
            view;
        });
        [self.view addSubview:subView];
        //小标题
        UILabel * topLabel = ({
            UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(0), ActualW((375/3.0)), ActualH(14))];
            label.textColor = RGB(255, 255, 255);
            label.textAlignment = NSTextAlignmentCenter;
            label.text = topArray[i];
            label.font = FontX(FONTSIZE(13));
            label;
        });
        [subView addSubview:topLabel];
        if (i == 0) {
            //金额
            self.moneyLabel = ({
                UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(21), ActualW((375/3.0)), ActualH(20))];
                label.textColor = RGB(255, 255, 255);
                label.textAlignment = NSTextAlignmentCenter;
                label.font = FontX(FONTSIZE(19));
                label;
            });
            [subView addSubview:self.moneyLabel];
            //金额子标题
            self.subMoneyLabel = ({
                UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(45), ActualW((375/3.0)), ActualH(12))];
                label.textColor = RGB(255, 255, 255);
                label.textAlignment = NSTextAlignmentCenter;
                label.font = FontX(FONTSIZE(11));
                label;
            });
            [subView addSubview:self.subMoneyLabel];
        }
        if (i == 1) {
            self.orderLabel = ({
                UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(21), ActualW((375/3.0)), ActualH(20))];
                label.textColor = RGB(255, 255, 255);
                label.textAlignment = NSTextAlignmentCenter;
                label.font = FontX(FONTSIZE(19));
                label;
            });
            [subView addSubview:self.orderLabel];
            //金额子标题
            self.subOrderLabel = ({
                UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(45), ActualW((375/3.0)), ActualH(12))];
                label.textColor = RGB(255, 255, 255);
                label.textAlignment = NSTextAlignmentCenter;
                label.font = FontX(FONTSIZE(11));
                label;
            });
            [subView addSubview:self.subOrderLabel];
        }
        if (i == 2) {
            self.turnoverLabel = ({
                UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(21), ActualW((375/3.0)), ActualH(20))];
                label.textColor = RGB(255, 255, 255);
                label.textAlignment = NSTextAlignmentCenter;
                label.font = FontX(FONTSIZE(19));
                label;
            });
            [subView addSubview:self.turnoverLabel];
            //金额子标题
            self.subTurnoverLabel = ({
                UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(45), ActualW((375/3.0)), ActualH(12))];
                label.textColor = RGB(255, 255, 255);
                label.textAlignment = NSTextAlignmentCenter;
                label.font = FontX(FONTSIZE(11));
                label;
            });
            [subView addSubview:self.subTurnoverLabel];
        }
    }
    
    self.moneyLabel.text = @"￥0.00";
    self.orderLabel.text = @"0";
    self.turnoverLabel.text = @"0";
    self.subMoneyLabel.text = [NSString stringWithFormat:@"可提现%@",@"0"];
    self.subOrderLabel.text = [NSString stringWithFormat:@"昨日%@单",@"0"];
    self.subTurnoverLabel.text = [NSString stringWithFormat:@"昨日%@",@"0"];
    //间隔线
    UIView * breakLine1 = ({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(124.5), ActualH(180.5), ActualW(1), ActualH(30))];
        view.backgroundColor = RGBA(255, 255, 255, 0.4);
        view;
    });
    [self.view addSubview:breakLine1];
    
    UIView * breakLine2 = ({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(249.5), ActualH(180.5), ActualW(1), ActualH(30))];
        view.backgroundColor = RGBA(255, 255, 255, 0.4);
        view;
    });
    [self.view addSubview:breakLine2];
    
    //下方按钮
    NSArray * imageArray = @[@"weijie",@"yijie",@"wancheng",@"quxiao",@"lianxi",@"shouhou",@"yaoqing",@"shangchuan"];
    NSArray * descArray = @[@"未接订单",@"已接订单",@"完成订单",@"取消订单",@"联系我们",@"退款售后",@"邀请入驻",@"商品上传"];
    for (int i = 0; i < 4; i++) {
        //底视图
        UIButton * stateBackBtn = ({
            UIButton * view = [[UIButton alloc] initWithFrame:CGRectMake(ActualW((375/3.0) * (i % 3)), ActualH(123.5) * (i / 3) + ActualH(245), ActualW((375/3.0)), ActualH(123.5))];
            view.backgroundColor = RGB(255, 255, 255);
            view.tag = 5000 + i;
            [view addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
            view;
        });
        [self.view addSubview:stateBackBtn];
        //图片
        UIImageView * stateImageView = ({
            UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(ActualW(47), ActualH(32), ActualW(31), ActualH(31))];
            imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", imageArray[i]]];
            imageView;
        });
        [stateBackBtn addSubview:stateImageView];
        //标题
        UILabel * stateLabel = ({
            UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(80), ActualW((375/3.0)), ActualH(14))];
            label.textColor = RGB(220, 220, 220);
            label.textAlignment = NSTextAlignmentCenter;
            label.text = descArray[i];
            label.font = FontX(FONTSIZE(13));
            label;
        });
        [stateBackBtn addSubview:stateLabel];
    }
    //分割线
    UIView * breakLine3 = ({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(123.5), ActualH(245), ActualW(2), ActualH(424))];
        view.backgroundColor = RGB(250, 250, 250);
        view;
    });
    [self.view addSubview:breakLine3];
    
    UIView * breakLine4 = ({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(248.5), ActualH(245), ActualW(2), ActualH(424))];
        view.backgroundColor = RGB(250, 250, 250);
        view;
    });
    [self.view addSubview:breakLine4];
    
    UIView * breakLine5 = ({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(368.5), ActualW(375), ActualH(2))];
        view.backgroundColor = RGB(250, 250, 250);
        view;
    });
    [self.view addSubview:breakLine5];
    
    UIView * breakLine6 = ({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(494), ActualW(375), ActualH(2))];
        view.backgroundColor = RGB(250, 250, 250);
        view;
    });
    [self.view addSubview:breakLine6];
    
}



#pragma mark - -------------------------下方事件----------------------------
- (void)clickAction:(UIButton *)sender {
    NSInteger index = sender.tag - 5000;
    
    OrderViewController * vc = [[OrderViewController alloc] init];
    switch (index) {
        case 0:
            vc.orderType = HYOrderTypeUnselected;
            [self.navigationController pushViewController:vc animated:YES];
            break;
        case 1:
            vc.orderType = HYOrderTypeSelected;
            [self.navigationController pushViewController:vc animated:YES];
            break;
        case 2:
            vc.orderType = HYOrderTypeCompleted;
            [self.navigationController pushViewController:vc animated:YES];
            break;
        case 3:
            vc.orderType = HYOrderTypeCanceled;
            [self.navigationController pushViewController:vc animated:YES];
            break;
        case 4:
            [self makeCall:@"021-64800139"];
            break;
        case 5:
            [self unopenedFunction:@"退款/售后"];
            break;
        case 6:
            [self unopenedFunction:@"邀请入驻"];
            break;
        case 7:
            [self unopenedFunction:@"商品上传"];
            break;
        default:
            break;
    }
    
}

- (void)unopenedFunction:(NSString *)typeTitle {
    NSString * title = [NSString stringWithFormat:@"%@功能暂未开放",typeTitle];
    NSString * message = @"我们会在下一个版本加入您需要的这些功能哦~";
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * submit = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:submit];
    [self presentViewController:alert animated:YES completion:nil];

}

- (void)makeCall:(NSString *)telNumber {
    //调用打电话，先提前开启alert
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:telNumber message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * submit = [UIAlertAction actionWithTitle:@"呼叫" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",telNumber]];
        if (kOSVersion < 10.0) {
            [[UIApplication sharedApplication] openURL:url];
        }else{
            [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
        }
    }];
    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:submit];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];

}


#pragma mark - -------------------------进入设置----------------------------
- (void)settingAction {
    MineInfoViewController * vc = [[MineInfoViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - -------------------------通知中心----------------------------
- (instancetype)init {
    if (self = [super init]) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:NOTIFICATION_LOGINSUCCESS object:nil];
    }
    return self;
}

- (void)dealloc {
    removeNotification(NOTIFICATION_LOGINSUCCESS);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
